﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Common;
using SYH.Config;

namespace SYH.Public.DAL
{
    public partial class Public_FriendLink
    {
        public Public_FriendLink()
        { }
        #region  基本方法
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Public_FriendLink");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;

            object obj = SqlHelper.ExecuteScalar(WebConfig.SqlConnString, CommandType.Text, strSql.ToString(), parameters);
            return Common.TypeParse.ObjectToInt(obj, 0) > 0;
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(SYH.Public.Model.Public_FriendLink model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Public_FriendLink(");
            strSql.Append("Title,LinkUrl,ImageUrl,Status,Sort,CreateTime)");
            strSql.Append(" values (");
            strSql.Append("@Title,@LinkUrl,@ImageUrl,@Status,@Sort,@CreateTime)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@Title", SqlDbType.NVarChar,50),
					new SqlParameter("@LinkUrl", SqlDbType.VarChar,50),
					new SqlParameter("@ImageUrl", SqlDbType.VarChar,50),
					new SqlParameter("@Status", SqlDbType.Int,4),
					new SqlParameter("@Sort", SqlDbType.Int,4),
					new SqlParameter("@CreateTime", SqlDbType.DateTime)};
            parameters[0].Value = model.Title;
            parameters[1].Value = model.LinkUrl;
            parameters[2].Value = model.ImageUrl;
            parameters[3].Value = model.Status;
            parameters[4].Value = model.Sort;
            parameters[5].Value = model.CreateTime;

            object obj = SqlHelper.ExecuteScalar(WebConfig.SqlConnString, CommandType.Text, strSql.ToString(), parameters);
            return Common.TypeParse.ObjectToInt(obj, 0);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(SYH.Public.Model.Public_FriendLink model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Public_FriendLink set ");
            strSql.Append("Title=@Title,");
            strSql.Append("LinkUrl=@LinkUrl,");
            strSql.Append("ImageUrl=@ImageUrl,");
            strSql.Append("Status=@Status,");
            strSql.Append("Sort=@Sort,");
            strSql.Append("CreateTime=@CreateTime");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Title", SqlDbType.NVarChar,50),
					new SqlParameter("@LinkUrl", SqlDbType.VarChar,50),
					new SqlParameter("@ImageUrl", SqlDbType.VarChar,50),
					new SqlParameter("@Status", SqlDbType.Int,4),
					new SqlParameter("@Sort", SqlDbType.Int,4),
					new SqlParameter("@CreateTime", SqlDbType.DateTime),
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = model.Title;
            parameters[1].Value = model.LinkUrl;
            parameters[2].Value = model.ImageUrl;
            parameters[3].Value = model.Status;
            parameters[4].Value = model.Sort;
            parameters[5].Value = model.CreateTime;
            parameters[6].Value = model.Id;

            int rows = SqlHelper.ExecuteNonQuery(WebConfig.SqlConnString, CommandType.Text, strSql.ToString(), parameters);
            return rows > 0;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Public_FriendLink ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;

            int rows = SqlHelper.ExecuteNonQuery(WebConfig.SqlConnString, CommandType.Text, strSql.ToString(), parameters);
            return rows > 0;
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string Idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Public_FriendLink ");
            strSql.Append(" where Id in (" + Idlist + ")  ");
            int rows = SqlHelper.ExecuteNonQuery(WebConfig.SqlConnString, CommandType.Text, strSql.ToString());
            return rows > 0;
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public SYH.Public.Model.Public_FriendLink GetModel(int Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 Id,Title,LinkUrl,ImageUrl,Status,Sort,CreateTime from Public_FriendLink ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;

            SYH.Public.Model.Public_FriendLink model = new SYH.Public.Model.Public_FriendLink();
            DataSet ds = SqlHelper.ExecuteDataset(WebConfig.SqlConnString, CommandType.Text, strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public SYH.Public.Model.Public_FriendLink DataRowToModel(DataRow row)
        {
            SYH.Public.Model.Public_FriendLink model = new SYH.Public.Model.Public_FriendLink();
            if (row != null)
            {
                if (row["Id"] != null && row["Id"].ToString() != "")
                {
                    model.Id = int.Parse(row["Id"].ToString());
                }
                if (row["Title"] != null)
                {
                    model.Title = row["Title"].ToString();
                }
                if (row["LinkUrl"] != null)
                {
                    model.LinkUrl = row["LinkUrl"].ToString();
                }
                if (row["ImageUrl"] != null)
                {
                    model.ImageUrl = row["ImageUrl"].ToString();
                }
                if (row["Status"] != null && row["Status"].ToString() != "")
                {
                    model.Status = int.Parse(row["Status"].ToString());
                }
                if (row["Sort"] != null && row["Sort"].ToString() != "")
                {
                    model.Sort = int.Parse(row["Sort"].ToString());
                }
                if (row["CreateTime"] != null && row["CreateTime"].ToString() != "")
                {
                    model.CreateTime = DateTime.Parse(row["CreateTime"].ToString());
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id,Title,LinkUrl,ImageUrl,Status,Sort,CreateTime ");
            strSql.Append(" FROM Public_FriendLink ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return SqlHelper.ExecuteDataset(WebConfig.SqlConnString, CommandType.Text, strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" Id,Title,LinkUrl,ImageUrl,Status,Sort,CreateTime ");
            strSql.Append(" FROM Public_FriendLink ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return SqlHelper.ExecuteDataset(WebConfig.SqlConnString, CommandType.Text, strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM Public_FriendLink ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = SqlHelper.ExecuteNonQuery(WebConfig.SqlConnString, CommandType.Text, strSql.ToString());
            return Common.TypeParse.ObjectToInt(obj, 0);
        }
        #endregion

        #region 更新一个字段
        /// <summary>
        /// 更新一个字段
        /// </summary>
        /// <param name="name">字段名</param>
        /// <param name="value">值</param>
        /// <param name="strWhere">条件</param>
        /// <param name="flag">字符串表示：true为字符串；false为整型、bool、日期等</param>
        /// <returns></returns>
        public bool UpdateOneField(string name, string value, string strWhere, bool flag)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("UPDATE Public_FriendLink set ");
            strSql.Append(" " + name + "= ");
            if (flag)
            {
                strSql.Append(" '" + value + "' ");
            }
            else
            {
                strSql.Append(" " + value + " ");
            }
            strSql.Append(" WHERE " + strWhere);

            return SqlHelper.ExecuteSql(Config.WebConfig.SqlConnString, strSql.ToString()) > 0;
        }
        #endregion

        #region 分页数据
        /// <summary>
        ///分页数据
        /// </summary>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <param name="strCondition"></param>
        /// <param name="pageCount"></param>
        /// <param name="recordCount"></param>
        /// <returns></returns>
        public DataTable GetPageData(int pageSize, int pageIndex, string strCondition, out int pageCount, out int recordCount)
        {
            DataTable dt = Common.PublicSqlData.SimplePager(WebConfig.SqlConnString,
                                                            "Public_FriendLink",
                                                            " [Id],[Title],[LinkUrl] ,[ImageUrl] ,[Status] ,[Sort],[CreateTime]",
                                                            pageSize,
                                                            pageIndex,
                                                            "Id",
                                                            true,
                                                            strCondition,
                                                            out pageCount,
                                                            out recordCount).Tables[0];

            return dt;
        }
        #endregion
    }
}
